package com.maxjay.system.service.impl;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.maxjay.common.exception.ERR;
import com.maxjay.common.exception.GeneException;
import com.maxjay.common.util.bean.BeanUtil;
import com.maxjay.common.util.date.DateUtil;
import com.maxjay.common.util.uuid.UUIDUtil;
import com.maxjay.system.core.cache.UserContextHolder;
import com.maxjay.system.dao.BaseDao;
import com.maxjay.system.po.BaseBusinessEntity;
import com.maxjay.system.po.BaseEntity;
import com.maxjay.system.service.BaseService;

@Service("baseService")
@Transactional
@SuppressWarnings({"rawtypes"})
public class BaseServiceImpl implements BaseService {

	private static Logger logger = Logger.getLogger(BaseServiceImpl.class);
	
	@Resource
	protected BaseDao baseDao;
	
	public BaseEntity get(Class clazz, String id) {
		BaseEntity baseEntity = (BaseEntity)baseDao.get(clazz, id);
		return baseEntity;
	}

	@Transactional(propagation=Propagation.REQUIRED)
	public void save(BaseEntity baseEntity) {
		baseEntity.setCreateTime(DateUtil.getCurrentDateStr(DateUtil.DB_STORE_DATE_FULL));
		baseEntity.setUpdateTime(DateUtil.getCurrentDateStr(DateUtil.DB_STORE_DATE_FULL));
		baseEntity.setUpdateKey(UUIDUtil.generate().substring(0, 8));
		if(baseEntity instanceof BaseBusinessEntity){
			BaseBusinessEntity baseBusinessEntity = (BaseBusinessEntity)baseEntity;
			baseBusinessEntity.setCreateUser(UserContextHolder.getCurrentUser());
			baseBusinessEntity.setCreateDeptCode(UserContextHolder.getCurrentUser().getDeptCode());
		}
		baseDao.save(baseEntity);
	}

	@Transactional(propagation=Propagation.REQUIRED)
	public void update(BaseEntity baseEntityVo) {
		BaseEntity baseEntityPo = (BaseEntity)this.get(baseEntityVo.getClass(), baseEntityVo.getObjId());
		BeanUtil.copyNotNullProperties(baseEntityPo, baseEntityVo);
		baseEntityPo.setUpdateTime(DateUtil.getCurrentDateStr(DateUtil.DB_STORE_DATE_FULL));
		baseDao.update(baseEntityPo);
	}

	@Transactional(propagation=Propagation.REQUIRED)
	public void saveOrUpdate(BaseEntity baseEntity) {
		if(StringUtils.isNotBlank(baseEntity.getObjId())){
			update(baseEntity);
		} else {
			save(baseEntity);
		}
	}
	
	@Transactional(propagation=Propagation.REQUIRED)
	public void saveOrValidateUpdate(BaseEntity baseEntity, String updateKey) {
		if(StringUtils.isNotBlank(baseEntity.getObjId())){
			validateUpdate(baseEntity, updateKey);
		} else {
			save(baseEntity);
		}
	}
	
	@Transactional(propagation=Propagation.REQUIRED)
	public void validateUpdate(BaseEntity baseEntityVo, String updateKey) {
		BaseEntity baseEntityPo = (BaseEntity)this.get(baseEntityVo.getClass(), baseEntityVo.getObjId());
		if(StringUtils.isNotBlank(updateKey) && updateKey.equals(baseEntityPo.getUpdateKey())){
			BeanUtil.copyNotNullProperties(baseEntityPo, baseEntityVo);
			baseDao.update(baseEntityPo);
		} else {
			logger.error("更新密钥不正确，无法执行修改");
			throw GeneException.getGeneException(ERR.ERROR_ERR_MSG, "更新密钥不正确，无法执行修改");
		}
	}

	@Transactional(propagation=Propagation.REQUIRED)
	public void delete(BaseEntity baseEntity) {
		baseDao.delete(baseEntity);
	}

	@Transactional(propagation=Propagation.REQUIRED)
	public void delete(Class clazz, String id) {
		baseDao.delete(clazz, id);
	}

	@Override
	public void validateDelete(Class clazz, String id, String updateKey) {
		BaseEntity baseEntityPo = (BaseEntity)this.get(clazz, id);
		if(StringUtils.isNotBlank(updateKey) && updateKey.equals(baseEntityPo.getUpdateKey())){
			this.delete(baseEntityPo);
		} else {
			logger.error("更新密钥不正确，无法执行删除");
			throw GeneException.getGeneException(ERR.ERROR_ERR_MSG, "更新密钥不正确，无法执行删除");
		}
	}

}
